import com.alibaba.fastjson2.JSONObject;
import socket.CmdMessage;
import socket.CmdMsgType;
import struct.enums.ResourceType;
import struct.ResourceConfig;
import lib4wrapper.WrapperRemoteConnector;

import java.util.List;

public class SensorDemo1 {
    public static void main(String[] args) {
        ResourceConfig config = new ResourceConfig("Sensor1", ResourceType.SENSOR, List.of("field1", "field2"));
        WrapperRemoteConnector connector = WrapperRemoteConnector.getInstance();
        if (connector.register("127.0.0.1", 9091, config)) { //内部封装，向平台发送register报文，并等待返回注册结果响应报文
            while (true) {
                CmdMessage msg = connector.recv(); //阻塞式接收平台发送的报文
                switch (msg.cmd) {
                    // 根据具体指令执行相应操作
                    case CmdMsgType.SENSORY_REQUEST:
                        JSONObject value = new JSONObject();
                        value.put("field1", "mock_data1");
                        value.put("field2", "mock_data2");
                        // 发送响应报文
                        CmdMessage response = new CmdMessage(CmdMsgType.SENSORY_BACK, value.toJSONString());
                        connector.send(response);
                        break;
                }
            }
        }
    }
}